/**
 * 
 */
package org.mtdownload.dialogs.preferences;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.mtdownload.utils.PreferencesManager;

/**
 * @author sergiu
 *
 */
public class PreferencesDialog extends JDialog implements PreferencesChangedListener{

	private static final long serialVersionUID = 3905806366038897205L;
	public boolean cancelled=true;
	private JPanel mainPanel=new JPanel();
	private JButton applyButton=new JButton("Apply");
	private PreferencePanel generalPrefs=new GeneralPreferencesPanel();
	private PreferencePanel colorsPrefs=new ProgressColors();
	private PreferencePanel ftpPrefs=new FTPPreferences();
	private PreferencePanel[] panels=null;

	/**
	 * @param owner
	 * @param title
	 * @param modal
	 * @throws HeadlessException
	 */
	public PreferencesDialog(Frame owner, String title, boolean modal)
			throws HeadlessException {
		super(owner, title, modal);
		init();
	}
	
	private void init(){
		setSize(600,450);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLayout(new BorderLayout());
		generalPrefs.setPreferencesListener(this);
		colorsPrefs.setPreferencesListener(this);
		ftpPrefs.setPreferencesListener(this);
		panels=new PreferencePanel[]{generalPrefs,ftpPrefs,colorsPrefs};
		
		JPanel buttonsPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton okButton=new JButton("OK");
		JButton cancelButton=new JButton("Cancel");						
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				ok();
			}
		});
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				cancel();
			}
		});
		applyButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				apply();
				applyButton.setEnabled(false);
			}
		});
		applyButton.setEnabled(false);		
		buttonsPanel.add(okButton);
		buttonsPanel.add(applyButton);
		buttonsPanel.add(cancelButton);
		setupPanels();		
		
		add(mainPanel,BorderLayout.CENTER);
		add(buttonsPanel,BorderLayout.SOUTH);
	}
	
	private void setupPanels(){
		final JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		final JList jlist=new JList(panels);
		jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		jlist.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting())return;
				Object obj=jlist.getSelectedValue();
				if(obj==null)return;
				split.setRightComponent((PreferencePanel)obj);
			}
		});
		split.setLeftComponent(jlist);
		split.setDividerLocation(100);
		if(panels.length>0){
			jlist.setSelectedIndex(0);
		}
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(split,BorderLayout.CENTER);
	}
	
	private void ok(){
		setCancelled(false);
		apply();
		dispose();
	}
	
	private void cancel(){
		setCancelled(true);
		dispose();
	}
	
	public void apply(){
		for(int i=0;i<panels.length;i++){
			panels[i].applyChanges();
		}
		PreferencesManager.savePreferences();		
	}

	/**
	 * @return Returns the cancelled.
	 */
	public boolean isCancelled() {
		return cancelled;
	}
	

	/**
	 * @param cancelled The cancelled to set.
	 */
	public void setCancelled(boolean cancelled) {
		this.cancelled = cancelled;
	}

	public void preferencesChanged() {
		applyButton.setEnabled(true);
	}	
}
